package com.yinp.proapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;


public abstract class CommonAdapter<T> extends RecyclerView.Adapter<SingleViewHolder> {

    protected List<T> dataList;
    protected LayoutInflater mInflater;

    private ComViewHolder.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(ComViewHolder.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public CommonAdapter(Context context, List<T> datalist) {
        this.dataList = datalist;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public SingleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        SingleViewHolder viewHolder = setComViewHolder(null, viewType, parent);
        viewHolder.onItemClickListener(onItemClickListener);
        return viewHolder;
    }

    protected abstract SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent);

    @Override
    public void onBindViewHolder(@NotNull SingleViewHolder holder, int position) {
        onBindItem(holder, position, dataList.get(holder.getAbsoluteAdapterPosition()));
    }

    public void onBindItem(SingleViewHolder holder, int position, T item) {
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }
}
